<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline label-width="68px">
      <el-form-item label="门店名称" prop="storeId">
        <el-select
          v-model="queryParams.storeId"
          placeholder="请选择门店名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="柜机名称" prop="cabinetId">
        <el-select
          v-model="queryParams.cabinetId"
          placeholder="请选择柜机名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in cabinetList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="异常类型" prop="describeType">
        <el-select
          v-model="queryParams.describeType"
          placeholder="请选择异常类型"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in dict.type.mc_order_abnormal_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="柜机名称" align="center" prop="cabinetName" />
      <el-table-column label="异常类型" align="center" prop="describeType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mc_order_abnormal_type"
            :value="scope.row.describeType"
          />
        </template>
      </el-table-column>
      <el-table-column label="处理类型" align="center" prop="processType" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'danger' : 'success'">
            {{ scope.row.status == 1 ? "未处理" : "已处理" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理记录" align="center" prop="remark" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['mc:abnormal:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            处理
          </el-button>
          <el-button
            v-hasPermi="['mc:abnormal:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异常反馈对话框 -->
    <el-dialog
      title="处理异常反馈"
      :visible.sync="open"
      width="1000px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="institutionName">
              {{ editForm.institutionName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="门店名称" prop="storeName">
              {{ editForm.storeName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="柜机名称" prop="cabinetName">
              {{ editForm.cabinetName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="异常类型" prop="describeType">
              <dict-tag
                :options="dict.type.mc_order_abnormal_type"
                :value="editForm.describeType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              {{ editForm.orderNo }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="处理方式" prop="processType">
          <el-select
            v-model="editForm.processType"
            placeholder="请选择处理方式"
            clearable
          >
            <el-option label="未开发" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理记录" prop="remark">
          <el-input v-model="editForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStore } from "@/api/mc/store";
import { listCabinet } from "@/api/mc/cabinet";
import {
  listAbnormal,
  getAbnormal,
  delAbnormal,
  addAbnormal,
  updateAbnormal,
} from "@/api/mc/abnormal";

export default {
  name: "Abnormal",
  dicts: ["mc_order_abnormal_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 异常反馈表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        cabinetId: null,
        describeType: null,
        processType: null,
        orderNo: null,
      },
      // 表单参数
      editForm: {},
      // 表单校验
      rules: {
        processType: [{ required: true, message: "请选择处理方式", trigger: "blur" }],
      },
      storeList: [], // 门店列表
      cabinetList: [], // 柜机列表
    };
  },
  created() {
    this.getStoreList();
    this.getCabinetList();
    this.getList();
  },
  methods: {
    /** 查询异常反馈列表 */
    getList() {
      this.loading = true;
      listAbnormal(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.editForm = {
        id: null,
        institutionId: null,
        storeId: null,
        cabinetId: null,
        describeType: null,
        processType: null,
        orderNo: null,
        status: null,
        remark: null,
      };
      this.resetForm("editForm");
    },
    // 获取门店列表
    getStoreList() {
      listStore({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.storeList = res.rows;
      });
    },
    // 获取柜机列表
    getCabinetList() {
      listCabinet({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.cabinetList = res.rows;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getAbnormal(row.id).then((res) => {
        this.editForm = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          const { id, processType, remark } = this.editForm;
          let data = { id, processType, status: 0, remark };
          updateAbnormal(data).then((res) => {
            this.$modal.msgSuccess("处理成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除选中的1条异常反馈吗？")
        .then(() => {
          delAbnormal(row.id).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
